package com.xqls.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.AlarmBean;
import com.xqls.dal.dao.AlarmDao;
import com.xqls.dal.model.Alarm;
import com.xqls.dal.model.AlarmExample;
import com.xqls.service.AlarmService;
import com.xqls.util.DictionaryEnums;
import com.xqls.util.ObjectUtil;
@Service
public class AlarmServiceImpl implements AlarmService {

	@Autowired private AlarmDao alarmDao;
	@Override
	public boolean insertAlarm(  String deviceNo, int alarmType,int times) {
		Alarm alarm = new Alarm();
		alarm.setAlarmStatus(DictionaryEnums.ALARM.getCode());
		alarm.setDeviceNo(deviceNo);
		alarm.setAlarmType(alarmType);
		alarm.setAlarmTime(new Date());
		alarm.setTimes(times);
		alarm.setInitialAlarmTime(new Date());
		int result = alarmDao.insert(alarm);
		return result>0;
	}
	@Override
	public boolean updateAlarmStatus(String deviceNo, int alarmType, int alarmStatus,int times) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("alarmStatus",alarmStatus);
		map.put("deviceNo", deviceNo);
		map.put("alarmType", alarmType);
		map.put("newAlarmStatus", DictionaryEnums.ALARM_SOLVE.getCode());
		map.put("times", times);
		int result = alarmDao.updateAlarmStatus(map);
		return result>0;
	}
	@Override
	public Map<String, Object> getMinTime(String deviceNo, int alarmType, String alarmTime,DictionaryEnums alarmStatus) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("alarmStatus", alarmStatus.getCode());
		map.put("deviceNo", deviceNo);
		map.put("alarmType", alarmType);
		map.put("alarmTime", alarmTime);
		return alarmDao.selectMinTime(map);
	}
	@Override
	public List<Map<String, Object>> selStatusAlarm(Map<String, Object> map) {
		return alarmDao.selStatusAlarm(map);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<AlarmBean> selAlarmByExample(AlarmExample example) {
		List<Alarm> list = alarmDao.getAlarmByExample(example);
		return ObjectUtil.transferList(list, AlarmBean.class);
	}
	@Override
	public boolean updateAlarmData(String deviceNo, int alarmType, DictionaryEnums alarmStatus, int times) {
		Alarm alarm = new Alarm();
		alarm.setAlarmStatus(alarmStatus.getCode());
		alarm.setAlarmTime(new Date());
		alarm.setAlarmType(alarmType);
		alarm.setTimes(times);
		alarm.setDeviceNo(deviceNo);
		int result = alarmDao.updateAlarmData(alarm);
		return result>0;
	}


}
